package openglsuperbible.Chapter3;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Matthew Nicholls
 */
public class Scissors {
    int width,height;
   
    public Scissors(int width, int height) {
        this.width = width;
        this.height = height;
        
    }
        
    
    public void init()
    {        
        resize();
    }
    
    
    public void resize()
    {
        glViewport(0, 0, width, height);	
    }
        
    
    public void render()
    {
	// Clear blue window
        glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        
        // Now set scissor to smaller red sub region
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        glScissor(100, 100, 600, 400);
        glEnable(GL_SCISSOR_TEST);
        glClear(GL_COLOR_BUFFER_BIT);
        
        // Finally, an even smaller green rectangle
        glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
        glScissor(200, 200, 400, 200);
        glClear(GL_COLOR_BUFFER_BIT);
        
        // Turn scissor back off for next render
        glDisable(GL_SCISSOR_TEST);
    }
    
    
    
    	public void start() {
		try {
			Display.setDisplayMode(new DisplayMode(width,height));
                        Display.setTitle("Scissor test");
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
		// init OpenGL here
		init();
		while (!Display.isCloseRequested()) {
			
			// render OpenGL here
			render();
			Display.update();
                        Display.sync(60);
                        Display.setVSyncEnabled(true); 
		}
		
		Display.destroy();
	}
	
        
        
        
	public static void main(String[] argv) {
		Scissors program = new Scissors(800,600);
		program.start();
	}
}
